#!/bin/sh

set -e

cmd="redis-dump -u ${JIANMU_HOST}:${JIANMU_PORT}"

if [[ -n ${JIANMU_PASSWORD} ]]; then
  cmd="${cmd} -a ${JIANMU_PASSWORD}"
fi

if [[ -n ${JIANMU_DATABASE} ]]; then
    cmd="${cmd} -d ${JIANMU_DATABASE}"
fi

if [[ -n ${JIANMU_KEY} ]]; then
    cmd="${cmd} -f ${JIANMU_KEY}"
fi

`$cmd > ${JM_SHARE_DIR}/redis`

if [[ ! -s ${JM_SHARE_DIR}/redis ]]; then
  echo "error: Export data is empty!"
  exit 1
fi

cat ${JM_SHARE_DIR}/redis

echo ""
echo "result file:"
echo -e "{\"redis_path\": \"${JM_SHARE_DIR}/redis\"}" > /usr/result_file
cat /usr/result_file